package com.LeeCode;

import java.util.HashMap;

/**
 * 罗马数字转整数
 */

public class Code013 {
    public static void main(String[] args) {
        String s = "IV";
        System.out.println(new Code013().romanToInt(s));
    }

    public int romanToInt(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        map.put('I', 1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);

        int ans = 0, preNum = map.get(s.charAt(0));
        for (int i = 1; i < s.length(); i++) {
            int curNum = map.get(s.charAt(i));
            if (curNum <= preNum) {
                ans += preNum;
            } else {
                ans -= preNum;
            }
            preNum = curNum;
        }
        ans += preNum;
        return ans;
    }
}
